package com.example.whm;



import java.util.ArrayList;
import java.util.List;

import org.holoeverywhere.LayoutInflater;
import org.holoeverywhere.app.Application;
import org.holoeverywhere.app.Fragment;
import org.holoeverywhere.preference.SharedPreferences;
import org.holoeverywhere.widget.CheckBox;
import org.holoeverywhere.widget.ListView;
import org.holoeverywhere.widget.Toast;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.MarginLayoutParams;
import android.widget.BaseAdapter;

import com.alibaba.fastjson.JSON;
import com.androidquery.AQuery;
import com.androidquery.callback.AjaxCallback;
import com.androidquery.callback.AjaxStatus;
import com.example.whm.ThreeFragment.PageAdapter;
import com.example.whm.ThreeFragmentItem.adapter2;
import com.example.whm.model.DateRange;
import com.example.whm.model.SubscribeItem;
import com.viewpagerindicator.CirclePageIndicator;

public class OneFragment extends Fragment{

	private AQuery aq;
	private AQuery aq2;
	
	private String topic;
	 ViewPager Pager;
	 CirclePageIndicator Indicator;
	 FragmentManager fm ;
	 PageAdapter adapter;
	 ViewPager Pager2;

    @Override
 	public View onCreateView(LayoutInflater inflater, ViewGroup container,
 			Bundle savedInstanceState) {
     	View view = inflater.inflate(R.layout.one_layout, container, false); 
     	aq=new AQuery(getActivity(), view);
     	 Pager = (ViewPager)view.findViewById(R.id.one_layout_prent_pager);
     	 
     	// Pager2 = (ViewPager)view.findViewById(R.id.one_layout_pager);
        // Indicator = (CirclePageIndicator)view.findViewById(R.id.one_layout_carousel_indicator);
        
     	
     	
     	
     	return view;
 	}
    
    



	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		
		 
		 fm = getChildFragmentManager();
		init();
		
	}


	
	
	public  class PageAdapter extends FragmentStatePagerAdapter{

		List<String> data;
		public PageAdapter(FragmentManager fm,List<String> da) {
			super(fm);
			this.data=da; 
		}
       
		
		@Override
		public Fragment getItem(int pos) {
			
			//AQUtility.debug("primary", topics[pos]);
			//TwoFragmentItem f=new TwoFragmentItem();
			Bundle args = new Bundle();
			
		/*	args.putString("id", data.get(pos).getId());
			args.putInt("pos", pos);
			if (pos==0) {
				
				args.putString("result", result);
			}*/
			OneFragmentCarousel fragment = (OneFragmentCarousel) Fragment.instantiate(OneFragmentCarousel.class, args);			
			return fragment;
			
		}

		@Override
		public int getCount() {
			return data.size();
		}
		

		
		
	}

	public  class PageAdapter2 extends FragmentStatePagerAdapter{

		List<String> data;
		String data2;
		public PageAdapter2(FragmentManager fm,List<String> da) {
			super(fm);
			this.data=da; 
		}
       
		
		
		
		public void setData2(String data2) {
			this.data2 = data2;
		}




		@Override
		public Fragment getItem(int pos) {
			
			//AQUtility.debug("primary", topics[pos]);
			//TwoFragmentItem f=new TwoFragmentItem();
			Bundle args = new Bundle();
			args.putString("data", data2); 
		/*	args.putString("id", data.get(pos).getId());
			args.putInt("pos", pos);
			if (pos==0) {
				
				args.putString("result", result);
			}*/
			System.out.println("data.get(pos)===="+data.get(pos));
			
			if (data.get(pos).equals("normal")) {
				System.out.println("normal");
				OneFragmentNormal fragment = (OneFragmentNormal) Fragment.instantiate(OneFragmentNormal.class, args);			
				return fragment;
			}
			else  {
				System.out.println("rank");
				OneFragmentRank fragment = (OneFragmentRank) Fragment.instantiate(OneFragmentRank.class, args);			
				return fragment;
			}
		
			/*	System.out.println("normal");
				OneFragmentNormal fragment = (OneFragmentNormal) Fragment.instantiate(OneFragmentNormal.class, args);			
				return fragment;*/
			
			
		}

		@Override
		public int getCount() {
			return data.size();
		}
		

		
		
	}
	
	
	
	
	
class adapter extends BaseAdapter{

		
		List<String> list;
		public adapter() {
			super();
			// TODO Auto-generated constructor stub
		}
	   
		
		
		
		public List<String> getList() {
			return list;
		}




		public void setList(List<String> list) {
			this.list = list;
		}




		public adapter(List<String> list) {
			super();
			this.list = list;
		}


		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return list.size();
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return list.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return arg0;
		}

		@Override
		public View getView(int arg0, View arg1, ViewGroup arg2) {
			// TODO Auto-generated method stub
		
			System.out.println("arg0---------------"+arg0);
			if(arg1 == null){
				arg1 = aq.inflate(arg1, R.layout.one_layout_gallery, arg2);
			}
			
			AQuery aq2= aq.recycle(arg1);
			
			/*SubscribeItem i = (SubscribeItem) getItem(arg0);
			ids.add(i.getId());
			ids_sub.add(i);
			
			AQuery aq = aq2.recycle(arg1);
			aq.id(R.id.three_layout_item_date).text(i.getDetail_date());
			aq.id(R.id.three_layout_item_price).text(String.valueOf(i.getPrice()));
			System.out.println("JSON.toJSONString(i.getList())--------"+JSON.toJSONString(i.getList()));
			aq.id(R.id.three_layout_item_grid).adapter(new adapter2(i.getList())).itemClicked(ThreeFragmentItem.this); 
		    checkb=(CheckBox) aq.id(R.id.three_layout_item_check).getCheckBox();
			checkb.setTag(arg0);
			checkb.setOnClickListener(ThreeFragmentItem.this);*/
		    return arg1;
		}
		
		
	}
	
	
	
	
	
	
	
	ListView list;
public void init(){
	
	  String url="http://"+Whm_app.api_url+":9000/api/indexp";
	  AjaxCallback<String> cb = new AjaxCallback<String>();        
	  cb.url(url).type(String.class).weakHandler(this, "ui");
	  SharedPreferences sp=((Application)getActivity().getApplication()).getSharedPreferences("whm",Context.MODE_PRIVATE);
  	String cookiename=sp.getString("cookie_name",null);
  	String cookievalue=sp.getString("cookie_value",null);
  	if (cookiename!=null&&cookievalue!=null) {
  		cb.cookie(cookiename,cookievalue);               
	}
	  aq.ajax(cb);
  }

  public void ui(String url, String r, AjaxStatus status){
	
	  //System.out.println("ui  s="+r); 
	  if (status.getCode()==200) {
		 
		  
		  final String s=r;
		  new Thread( new Runnable() {
			  @Override
			public void run() {
				  // String s1= JSON.parseObject(s).getString("users");
				  
				/*  String mapr= JSON.parseObject(s).getString("mapr");
				  String listj= JSON.parseObject(s).getString("list");
				  map= JSON.parseObject(mapr, Map.class);
				  
				  boxs= JSON.parseArray(listj, PreOrder.class);
				  System.out.println("boxs.size()========="+boxs.size());*/
				
				  getActivity().runOnUiThread(new Runnable() {
					  
					  @Override
					  public void run() {
						  
						 
						   List<String> l2=new ArrayList<String>();
						   l2.add("normal");
						   l2.add("rank");
						   
						   PageAdapter2 adapter2 = new PageAdapter2(fm,l2);
						   adapter2.setData2(s);
						   Pager.setAdapter(adapter2);	
						  
						  
						  
						  
						  
						  
						  
						  
						 /* Toast.makeText(getActivity(), "boxs.size()="+boxs.size(), 1000).show();
						  if (isload) {
							  show_dialog(false);
							 
							  adap.setList(boxs);
							  adap.notifyDataSetChanged();
						}else{		
							isload=true;
						  aq2.id(R.id.progress).gone();
						//  aq2.id(R.id.four_layout_line).visible();
						  
						//   list=(ListView) aq2.id(R.id.myorder_fragment_listview).getListView();
						  View v=getActivity().getLayoutInflater().inflate(R.layout.four_layout_header, null);
						  View v2=getActivity().getLayoutInflater().inflate(R.layout.four_layout_footer, null);
						   AQuery a=new AQuery(v);
						  AQuery a2=new AQuery(v2);
						  a.id(R.id.four_layout_contact_name).text(add.getContact_name());
						  a.id(R.id.four_layout_contact_phone).text(add.getPhone());
						  a.id(R.id.four_layout_contact_addr).text(add.getDetail_address());
						  a2.find(R.id.four_layout_btn_sure).clicked(MyOrderFragment.this); 
						   radioButtonGroup=(RadioGroup) a.find(R.id.four_layout_pay).getView();
						  list.addHeaderView(v);
						  list.addFooterView(v2);
						 
						  adap=new adapter(boxs);
						  aq2.id(R.id.myorder_fragment_listview).adapter(adap);
						  aq2.id(R.id.myorder_fragment_listview).itemClicked(MyPreOrderFragment.this);
						  aq2.id(R.id.myorder_fragment_listview).getListView().setOnItemLongClickListener(MyPreOrderFragment.this);
						
						
						
						
						}*/
						  
						  
						  
						  
						  
						  
						  
						  
						  
						  
						  
						  //Utility.setListViewHeightBasedOnChildren(list);
					  }
				  });
			  }
		  }).start();
			
		  
	}else{
		Toast.makeText(getActivity(), "request error", 1000).show();
		
	}
	  
  }

  
  
    
    
    
    
    
    
    
    
    
    
    
    
 
}
